package com.xafc.demo.sys.controller.api;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xafc.demo.sys.model.dto.UserDto;
import com.xafc.demo.sys.model.entity.User;
import com.xafc.demo.framework.result.ApiResult;
import com.xafc.demo.framework.result.PageResult;
import com.xafc.demo.framework.result.ResultCode;
import com.xafc.demo.sys.model.query.UserPageQuery;
import com.xafc.demo.sys.service.IUserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

@Api(tags="用户相关接口")
@RestController
@RequestMapping("/api/sys/user")
public class UserApi {

    @Autowired
    private IUserService userService;

//    @GetMapping("/list")
//    public PageResult<User> list(UserPageQuery query){
//        Page<User> page = this.userService.getUserList(query);
//        return PageResult.success(page);
//    }

    @ApiOperation(value = "根据分页获取用户列表")
    @PreAuthorize("hasAuthority('API_SYS_USER_LIST')")
    @GetMapping("/list")
    public PageResult<UserDto> list(UserPageQuery query){
//        Page<UserDto> page = this.userService.getUserListByWrapper(query);
        Page<UserDto> page = this.userService.getUserList(query);
        return PageResult.success(page);
    }

    @ApiOperation(value = "根据ID获取用户信息")
    @PreAuthorize("hasAuthority('API_SYS_USER_UPDATE')")
    @GetMapping("/get/{id}")
    public ApiResult<User> detail(@PathVariable("id") Integer id){
        User user = userService.getUserById(id);
        return ApiResult.success(user);
    }

    @ApiOperation(value = "根据IDs获取多个用户信息")
    @GetMapping("/get")
    public ApiResult<List<User>> get(@RequestParam("ids") String ids){
        List<Integer> idLists = Arrays.stream(ids.split(",")).map(o -> Integer.valueOf(String.valueOf(o))).collect(Collectors.toList());
        List<User> userList = userService.getUserByIds(idLists);
        return ApiResult.success(userList);
    }

    @ApiOperation(value = "新增用户")
    @PreAuthorize("hasAuthority('API_SYS_USER_ADD')")
    @PostMapping("/add")
    public ApiResult add(User user){
        ResultCode resultCode = userService.addUser(user);
        return ApiResult.value(resultCode);
    }

    @ApiOperation(value = "更新用户")
    @PreAuthorize("hasAuthority('API_SYS_USER_UPDATE')")
    @PostMapping("/update")
    public ApiResult edit(User user){
        ResultCode resultCode = userService.updateUser(user);
        return ApiResult.value(resultCode);
    }

    @ApiOperation(value = "删除用户")
    @PreAuthorize("hasAuthority('API_SYS_USER_DELETE')")
    @PostMapping("/delete")
    public ApiResult delete(@RequestParam("id") Integer id){
        ResultCode resultCode = userService.deleteUser(id);
        return ApiResult.value(resultCode);
    }

}
